package com.cs.bd.ad;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.cs.bd.ad.self.WapsAdsSdkHelper;
import com.cs.bd.ad.bean.AdInfoBean;
import com.cs.bd.ad.http.bean.BaseModuleDataItemBean;
import com.cs.bd.ad.params.AdSdkParamsBuilder;
import com.cs.bd.ad.params.ClientParams;
import com.cs.bd.ad.sdk.bean.SdkAdSourceAdWrapper;
import com.cs.bd.ad.self.AdIdHelper;
import com.cs.bd.ad.self.request.NativeAdFactory;
import com.cs.bd.utils.LogUtils;
import com.cs.bd.utils.MachineUtils;
import com.cs.statistic.StatisticsManager;
import com.miui.zeus.mimo.sdk.MimoSdk;

public class AdSdkApi {

    public static final String ENTRANCE_ID_MAIN = "1";

    private static String sChannel;

    public static ClientParams sParams;

    private static Activity sActivity;

    public static boolean sDebug;

    public static boolean canLoadMopubScreenOff(Context context, int position) {
        return false;
    }

    public static void clickAdvertWithDialog(Context context, AdInfoBean adInfoBean, String tabCategory, String remark, boolean isShowDialog) {
    }


    public static void showAdvert(Context context, AdInfoBean adInfoBean, String tabCategory, String remark) {
    }


    public static void setSdkThreadPool(int poolSize) {
    }

    public static void loadAdBean(final AdSdkParamsBuilder adSdkParams) {
        if (!Activity.class.isInstance(adSdkParams.mContext) && sActivity != null) {
            adSdkParams.mContext = sActivity;
        }

        if (Activity.class.isInstance(adSdkParams.mContext)) {
            WapsAdsSdkHelper.initAdInfo((Activity) adSdkParams.mContext);
        }
        if (NativeAdFactory.canRequestAds(adSdkParams, sChannel)) {
            LogUtils.d("request : " + adSdkParams.mVirtualModuleId);
            NativeAdFactory.createAdsRequest(adSdkParams, sChannel).requestAds();
        }
    }

    public static int calculateCDays(Context context, long installTime) {
        return (int) ((TimerUtils.currentTimeMillis(context) - installTime) / (24 * 1000 * 60 * 60));
    }

    public static void setEnableLog(boolean onOff) {
        sDebug = onOff;
        LogUtils.setEnableLog(onOff);
        if (onOff) {
            MimoSdk.setDebugOn();
        }
    }

    public static void initSDK(Context context, String processName, String goId, String googleId, String channel, ClientParams params) {
        sChannel = channel;
        sParams = params;
        if (Application.class.isInstance(context)) {
            ((Application) context).registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                    sActivity = activity;
                }

                @Override
                public void onActivityStarted(Activity activity) {

                }

                @Override
                public void onActivityResumed(Activity activity) {
                    StatisticsManager.getInstance(activity).onResume(activity);
                }

                @Override
                public void onActivityPaused(Activity activity) {
                    StatisticsManager.getInstance(activity).onPause(activity);
                }

                @Override
                public void onActivityStopped(Activity activity) {

                }

                @Override
                public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

                }

                @Override
                public void onActivityDestroyed(Activity activity) {

                }
            });
        }

        LogUtils.d("sChannel: " + sChannel);
        WapsAdsSdkHelper.setChannel(channel);
        MimoSdk.init(context, AdIdHelper.getXmAppId(context), "fake_app_key", "fake_app_token");
        TimerUtils.forceRefresh(context);
    }

    public static void setClientParams(Context context, ClientParams params) {
    }

    public static void sdkAdClickStatistic(Context context, BaseModuleDataItemBean baseModuleDataItemBean, SdkAdSourceAdWrapper adWrapper, String tabCategory) {
    }

    public static void sdkAdShowStatistic(Context context, BaseModuleDataItemBean baseModuleDataItemBean, SdkAdSourceAdWrapper adWrapper, String tabCategory) {
    }

    public static boolean isNoad(Context context) {
        if (MachineUtils.hasSimCard(context) && MachineUtils.isWifiEnable(context)) {
            return false;
        }
        return true;
    }

    public static void startActivity(Context context, Intent intent) {
        try {
            if (!Activity.class.isInstance(context)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void clickAdvertWithToast(Context context, AdInfoBean mAdInfoBean, String s, String mUserType, boolean b, boolean b1) {

    }
}
